count1 = {}
with open("sample.txt","r",encoding="utf8") as f:
    flst = f.read().lower().split()
    for word in flst:
        if word in count1:
            count1[word] = count1[word] + 1
        else:
            count1[word] = 1
count2 = sorted(count1.items(),key = lambda x:x[1],reverse = True)
print(count2[:10])

with open("sample.txt","r",encoding="utf8") as f:
    fstr = f.read().replace('.',' ').replace('(',' ').replace(')',' ') # 多次replace性能会差
    flst = fstr.lower().split()
    for word in flst:
        if word in count1:
            count1[word] = count1[word] + 1
        else:
            count1[word] = 1
count2 = sorted(count1.items(), key=lambda x: x[1], reverse=True) print(count2[:10])


# 文件的读取可以按行读取，保证性能，因为如果文件太大的话，会占用大量的内存空间, 单词的分词，可以做优化
